<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
// import moment from 'moment';
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {},
  data() {
    return {
      appWindow: window,
      form: {
        name: "",
        // 活动名称
        activityName: "",
        // 固定时间
        activityTimeType: 1,
        // 开始时间 - 结束时间
        startTimeStr: "", //当前时间
        endTimeStr: "",
        // 下单可用优惠/抵扣
        deductOverlay: "",
        deductOverlayLst: [],
        // 活动状态
        activityStatus: 1,
        value: [],
      },

      // 页面接口地址
      apiUrl: {
        create: "api/time-discount/createTimeDiscount",
        update: "api/time-discount/updateTimeDiscount",
        detail: "api/time-discount/getTimeDiscount",
      },
      deducteList: [],

      isDisabled: false,
    };
  },
  methods: {
    // form 提交添加/修改 的参数扩展方法
    mixinEdit_editFormSubmitExtend(data_) {
      data_.startTimeStr = this.$vFN.Utils.timestampToDate("yyyy-MM-dd HH:mm:ss", data_.value[0]);
      data_.endTimeStr = this.$vFN.Utils.timestampToDate("yyyy-MM-dd HH:mm:ss", data_.value[1]);
      return data_;
    },
    // 编辑修改的参数扩展方法
    mixinEdit_editDataExtend(data_) {
      // 添加
      if (this.mixinEdit_editType == "add") {
        this.isDisabled = false;
      }
      // 修改
      else if (this.mixinEdit_editType == "update") {
        if (data_.activityStatus === 2) {
          this.isDisabled = true;
        }

        this.disposeIntegralType(data_);

        data_.value = [data_.startTimeStr, data_.endTimeStr]
      } else if (this.mixinEdit_editType == "copy") {
        this.isDisabled = false;

        this.disposeIntegralType(data_);

        data_.value = [data_.startTimeStr, data_.endTimeStr]
      }
      return data_;
    },

    async getIntegralType() {
      this.mixinEdit_loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: "api/time-discount/getIntegralType",
        },
        window
      );
      this.mixinEdit_loading = false;
      if (res._ok) {
        this.deducteList = res.data.map((item) => {
          return {
            ...item,
            isChecked: false,
          };
        });
        console.log("deducteList", this.deducteList);
      }
    },

    // 处理返回的资产id数组  判断是否选中
    disposeIntegralType(data_) {
      this.form.deductOverlayLst = data_.deductOverlayLst;
      // console.log("this.deducteList", this.deducteList);
      // console.log("this.form.deductOverlayLst", this.form.deductOverlayLst);

      this.deducteList.forEach((item) => {
        item.id = String(item.id);
        let index = this.form.deductOverlayLst.indexOf(item.id);
        // console.log("index", index);
        if (index > -1) {
          this.deducteList[index].isChecked = true;
        }
      });

      // console.log("this.deducteList", this.deducteList);
    },

    // 初始化
    async init() {
      await this.getIntegralType();
      // 初始化数据
      await this.mixinEdit_editBoxInit({
        model: "form",
        id: this.id,
      });
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>